package com.crl.module.live
{
	import com.crl.utils.FMSUtil;
	
	import flash.events.EventDispatcher;
	import flash.media.Video;
	import flash.net.NetStream;
	
	
	
	/**
	 * 单个直播对像,对于一个关联视频,它可以当做接收者也可以是发布者;
	 * @author crl
	 * 
	 */	
	public class SingleLive extends EventDispatcher
	{
		
		public var sharerConnection:SharerConnection;
		
		private var publisher:Publisher;
		private var receiver:Receiver;
		
		
		/**
		 * 共享视频数组; (一个流可以对应多个视频.也由此会出现声音重叠);
		 */		
		private var videos:Array;		
		
		/**
		 * 
		 * @param sharerConnection 必要参数,不能为空.共享连接对像;
		 * 
		 */		
		public function SingleLive(sharerConnection:SharerConnection)
		{
			if(sharerConnection==null){
				throw new Error("sharerConnection is null");
			}
			this.sharerConnection=sharerConnection;
			
			publisher=new Publisher(this);
			receiver=new Receiver(this);
		}
		
		
		/**
		 * 取得发布流,用于嵌入发布动作; 
		 * @return 
		 * 
		 */		
		public function get publishStream():NetStream{
			return publisher.getStream();
		}
		
		public function get currentStream():NetStream{
			if(receiver.running)return this.receiver.getStream();
			
			return this.publisher.getStream();
		}
		
		
		/**
		 * 设置是否发布视频; 
		 * @param bool
		 * 
		 */		
		public function setPublisherVideoStatus(bool:Boolean):void{
			this.publisher.videoStatus=bool;
		}
		
		
		/**
		 * 当然是否接收及发布视频; 
		 */		
		private var _videoStatus:Boolean=true;
		private var _soundVolume:Number=1;
		
		/**
		 * 视频开关; 
		 * @param bool
		 * 
		 */		
		public function set videoStatus(bool:Boolean):void{
			if(this.receiver.running)this.receiver.videoStatus=bool;
			if(this.publisher.running)this.publisher.videoStatus=bool;
			
			_videoStatus=bool;
		}
		
		public function get videoStatus():Boolean{
			return _videoStatus;
		}
		
		public function get volume():Number{
			return _soundVolume;
		}
		
		/**
		 * 设置音量; 
		 * @param value
		 * 
		 */		
		public function set volume(value:Number):void{
			_soundVolume=value;
			
			if(publisher.running)publisher.volume=value;
			if(receiver.running)receiver.volume=value;
		}
		
		
		
		
		/**
		 * 发布视频; 
		 * @param name 发布流名称;
		 * @param ktvKey 标识;
		 * @param isOpenVideo 是否开启视频;
		 * 
		 */		
		public function publish(name:String, ktvKey:String,isOpenVideo:Boolean=true):void{
			if(receiver.running)receiver.close();
			
			_videoStatus=isOpenVideo;
			
			publisher.publish(name,ktvKey,_videoStatus);
		}
		
		/**
		 * 播放一个视频; 
		 * @param name
		 * 
		 */		
		public function play(name:String):void{
			if(publisher.running)publisher.close();
			
			receiver.play(name);
		}
		
		/**
		 * 停止，不管停止什么 
		 * 
		 */		
		public function stop():void{
			if(publisher.running)publisher.close();
			if(receiver.running)receiver.close();
		}
		
		
		/**
		 * 注销此对像; 
		 * 
		 */		
		public function dispose():void{
			this.stop();
			
			publisher.dispose();
			receiver.dispose();
		}

		/**
         * 为多个显示提供支持; 
         * @param value
         * 
         */        
        public function setVideos(value:Array):void{
        	var item:Video;
        	if(this.videos){
	        	for each(item in videos){
	        		FMSUtil.closeVideo(item);
	        	}
        	}
        	
        	for each(item in value){
        		item.deblocking=0;
        	}
        	
        	this.videos=value;
        }
		
		
		/**
		 * 开始播放流 
		 * @param stream
		 * 
		 */		
		internal function showVideos(stream:*):void{
        	if(videos==null){
        		return;
        	}
        	
        	for each(var item:Video in videos){
        		FMSUtil.showVideo(item, stream);
        	}
        }
        
        
        internal function getVideos():Array{
        	return this.videos;
        }
        
		
		/**
         * 关闭视频显示; 
         * 
         */        
        internal function closeVideos():void{
        	if(videos==null || videos.length==0)return;
        	
        	for each(var item:Video in videos){
        		FMSUtil.closeVideo(item);
        	}
        }
		
		
	}
}